package controllers

import (
	"errors"
	"mdblog/models"
)

type BlogAdminApiController struct {
	AjaxController
}

func (o *BlogAdminApiController) Prepare() {
	if o.Ctx.Request.RequestURI != AdminApiLoginPrefix {
		if u := o.GetSession("user"); u == nil {
			o.ErrorResponse(errors.New("need login"))
			return
		}
	}
	o.Data["Configs"], _ = models.GetAllConfigs()
}

func (o *BlogAdminApiController) ClassifyCreate() {
	request := CreateClassifyRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.MessageResponse("ok")
		}
	}
}

func (o *BlogAdminApiController) ClassifyQuery() {
	request := PaginatorClassifyRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		all, err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.DataResponse(all)
		}
	}
}

func (o *BlogAdminApiController) ClassifyQueryAll() {
	request := QueryClassifyAllRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		all, err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.DataResponse(all)
		}
	}
}

func (o *BlogAdminApiController) TargetQuery() {
	request := PaginatorTargetsRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		all, err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.DataResponse(all)
		}
	}
}

func (o *BlogAdminApiController) TargetAll() {
	request := AllTargetsRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		all, err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.DataResponse(all)
		}
	}
}

func (o *BlogAdminApiController) TargetCreate() {
	request := CreateTargetsRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.MessageResponse("ok")
		}
	}
}

func (o *BlogAdminApiController) ClassifyTargetQuery() {
	request := ClassifyTargetsQueryRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		all, err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.DataResponse(all)
		}
	}
}

func (o *BlogAdminApiController) ArticleCreate() {
	request := ArticleCreateRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err = request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.MessageResponse("ok")
		}
	}
}

func (o *BlogAdminApiController) ArticleUpdate() {
	request := ArticleUpdateRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err = request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.MessageResponse("ok")
		}
	}
}

func (o *BlogAdminApiController) ArticleDelete() {
	id, err := o.GetInt(":id")
	if err != nil || id == 0 {
		o.ErrorResponse(errors.New("error params"))
		return
	}
	request := ArticleDeleteRequest{Id: id}
	err = request.Do()
	if err != nil {
		o.ErrorResponse(err)
	} else {
		o.MessageResponse("ok")
	}
}

func (o *BlogAdminApiController) ArticleQuery() {
	request := ArticleQueryPaginatorRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		articles, total, err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.DataTableResponse(articles, total, "ok")
		}
	}
}

func (o *BlogAdminApiController) SiteConfigsPaginator() {
	request := SiteConfigsPaginatorRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		articles, total, err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.DataTableResponse(articles, total, "ok")
		}
	}
}

func (o *BlogAdminApiController) SiteConfigCreate() {
	request := SiteConfigCreateRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.MessageResponse("ok")
		}
	}
}

func (o *BlogAdminApiController) SiteConfigUpdate() {
	request := SiteConfigUpdateRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.MessageResponse("ok")
		}
	}
}

func (o *BlogAdminApiController) ResourceUpload() {
	request := ResourceUpdateRequest{}
	path, err := request.Do(o)
	if err != nil {
		o.Data["json"] = map[string]interface{}{"success": 0, "message": err.Error(), "url": ""}
	} else {
		o.Data["json"] = map[string]interface{}{"success": 1, "message": "", "url": path}
	}
	o.ServeJSON()
}

func (o *BlogAdminApiController) ResourceRemove() {
	request := RemoveResourceRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.MessageResponse("ok")
		}
	}
}

func (o *BlogAdminApiController) ResourcePaginator() {
	request := ResourcePaginatorRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		resources, total, err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.DataTableResponse(resources, total, "ok")
		}
	}
}

func (o *BlogAdminApiController) NoticePaginator() {
	request := NoticePaginatorRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		notice, total, err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.DataTableResponse(notice, total, "ok")
		}
	}
}

func (o *BlogAdminApiController) NoticeCreate() {
	request := CreateNoticeRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.MessageResponse("ok")
		}
	}
}

func (o *BlogAdminApiController) NoticeUpdate() {
	id, _ := o.GetInt(":id")
	if id == 0 {
		o.Abort("404")
	}
	request := UpdateNoticeRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err := request.Do(id)
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.MessageResponse("ok")
		}
	}
}

func (o *BlogAdminApiController) CarouselPaginator() {
	request := CarouselPaginatorRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		all, total, err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.DataTableResponse(all, total, "ok")
		}
	}
}

func (o *BlogAdminApiController) CarouselEnableUpdate() {
	request := CarouselEnableChangeRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.MessageResponse("ok")
		}
	}
}

func (o *BlogAdminApiController) CarouselRemove() {
	request := CarouseRemoveRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.MessageResponse("ok")
		}
	}
}
func (o *BlogAdminApiController) CarouselCreate() {
	request := CarouselCreateRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err := request.Do()
		if err != nil {
			o.ErrorResponse(err)
		} else {
			o.MessageResponse("ok")
		}
	}
}
